import com.cty.Main;
import com.cty.Repository.UserRepository;
import com.cty.pojo.Address;
import com.cty.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;

@SpringBootTest(classes = Main.class)
public class test {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void test01() {
        User user = new User();
        user.setUserName("张三");
        user.setPassword("123456");
        Address address1 = new Address();
        address1.setAddress("北京");
        Address address2 = new Address();
        address2.setAddress("上海");
        Address address3 = new Address();
        address3.setAddress("天津");
        Address address4 = new Address();
        address4.setAddress("南京");
        ArrayList<Address> addresses = new ArrayList<>();
        addresses.add(address1);
        addresses.add(address2);
        addresses.add(address3);
        addresses.add(address4);

        // 单向关系
        user.setAddresses(addresses);

        userRepository.save(user);
    }
    @Test
    public void test02() {
        User user = new User();
        user.setUserName("张三");
        user.setPassword("123456");
        Address address1 = new Address();
        address1.setAddress("北京");
        Address address2 = new Address();
        address2.setAddress("上海");
        Address address3 = new Address();
        address3.setAddress("天津");
        Address address4 = new Address();
        address4.setAddress("南京");
        ArrayList<Address> addresses = new ArrayList<>();
        address1.setUser(user);
        address2.setUser(user);
        address3.setUser(user);
        address4.setUser(user);
        addresses.add(address1);
        addresses.add(address2);
        addresses.add(address3);
        addresses.add(address4);

        // 双向关系
        user.setAddresses(addresses);

        userRepository.save(user);
    }
}
